package com.hzj.act;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Dictionary;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.hzj.mybmi.R;

public class BMIAct extends Activity
{
	
	//BMI = 體重(公斤) / 身高2(公尺2)

	TextView resultValue;
	TextView resultText;
	TextView defauleText;

	Spinner sex;
	Spinner height;
	Spinner weight;

	
	OnItemSelectedListener onItemSelectedListener=new OnItemSelectedListener()
	
	{

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
		{
			
			
			
			
			 double resultTemp=   Integer.parseInt(weight.getSelectedItem().toString())/Math.pow(Integer.parseInt(height.getSelectedItem().toString())*0.01, 2);
			 DecimalFormat decimalFormat=new DecimalFormat("##.##");
			 resultValue.setText(decimalFormat.format(resultTemp) );   
			 
			 
			 
			 
			   if( sex.getSelectedItem().toString().equals(getResources().getStringArray(R.array.sex_values)[0]))
			   {
				   resultText.setText("男人");
				   defauleText.setText("完美数值");
			   }
			   else
			   {
				   resultText.setText("女人");
				   defauleText.setText("完美数值");
			   }
			   
			 
			 
			 
			
			
			
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent)
		{
			
		}
		
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		// 去掉标题栏
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.bmi_act);
		setTitle(R.string.act_title);

		resultValue = (TextView) findViewById(R.id.resultValue);
		resultText = (TextView) findViewById(R.id.resultText);
		defauleText = (TextView) findViewById(R.id.defaultText);

		sex = (Spinner) findViewById(R.id.sex);  
		// 初始化适配器
		ArrayAdapter<String> arrayAdapterSex = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,this.getResources().getStringArray(R.array.sex_values));
		arrayAdapterSex.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sex.setAdapter(arrayAdapterSex);
		sex.setOnItemSelectedListener(onItemSelectedListener);
		


		height = (Spinner) findViewById(R.id.height);
		Integer[] heights = new Integer[250 - 50 + 1];
		for (int i = 50; i <= 250; i++)
		{    
			heights[i - 50] = i;
		}
		ArrayAdapter<Integer> arrayAdapterHeight = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item,heights);
		arrayAdapterHeight.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		height.setAdapter(arrayAdapterHeight);
		height.setOnItemSelectedListener(onItemSelectedListener);

		weight = (Spinner) findViewById(R.id.weight);

		Integer[] weights = new Integer[400 - 20 + 1];
		for (int i = 20; i <= 400; i++)
		{
			weights[i - 20] = i;
		}
		ArrayAdapter<Integer> arrayAdapterWeight = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item,weights);
		arrayAdapterWeight.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		weight.setAdapter(arrayAdapterWeight);
		weight.setOnItemSelectedListener(onItemSelectedListener);
	}

}
